// 闭包


// 所有的衬衫颜色
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum ShirtColor{
    Red,
    Blue,
}

// 库存
pub struct Inventory{
    pub shirts:Vec<ShirtColor>
}

impl Inventory {

    pub fn giveaway(&self,user_preference:Option<ShirtColor>) -> ShirtColor{
        user_preference.unwrap_or_else(|| self.most_stocked())
    }
    
    // 返回最畅销的衬衫颜色
    pub fn most_stocked(&self)-> ShirtColor{
        let mut num_red = 0;
        let mut num_blue = 0;
        for ele in &self.shirts {
            match ele {
                ShirtColor::Red => num_red += 1,
                ShirtColor::Blue => num_blue += 1,
            }
        }
        if num_red > num_blue {
            ShirtColor::Red
        } else {
            ShirtColor::Blue

        }
    }
}


// 相当于 Consumer
pub fn do_with<F,T>(f:F,t:T)
where F:Fn(T),
{
    f(t);
}

// 相当于Function
pub fn map_value<F,T,R>(f:F,t:T)->R 
where F:Fn(T)->R,
{
    f(t)
}

// 相当于Supplier
pub fn get_with<F,R>(f:F)->R 
where F:Fn()->R,
{
    f()
}

// 相当于Predicate
pub fn do_predicate<F,T>(f:F,t:T)->bool
where F:Fn(T)->bool,
{
    f(t)
}